package com.bcinfo.iccard;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

import org.apache.log4j.Logger;
import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.executor.ExecutorFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

import com.bcinfo.iccard.codec.ISO8583CodecFactory;
import com.bcinfo.iccard.msg.ISOMessageHandler;

public class MinaServer {
	private static final int PORT = 11100;// 定义监听端口 
	private static Logger _logger=Logger.getLogger(MinaServer.class);
    public static void main(String[] args) throws IOException {  
        //创建一个非阻塞的Server端socket，用NIO  
        IoAcceptor acceptor = new NioSocketAcceptor(Runtime.getRuntime().availableProcessors()+1);  
        //创建接受数据的过滤器  
        DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();  
        //设定这个过滤器将一行一行的读取数据  
        chain.addLast("codec",new ProtocolCodecFilter(new ISO8583CodecFactory()));// 指定编码过滤器  
        chain.addLast("threadPool", new ExecutorFilter(Executors.newCachedThreadPool()));
        // 指定业务逻辑处理器  
        acceptor.setHandler(new ISOMessageHandler());  
        // 设置端口号  
        acceptor.setDefaultLocalAddress(new InetSocketAddress(PORT));  
        acceptor.bind(); //启动监听  
        _logger.info("Mina Server started, Listen on:"+PORT);  
    }
}
